const router = require('koa-router')()
const koa2Req = require('koa2-request');
const axios = require("axios");
const { login, getImages, getServers, getNetworks, deleteImage, createImage, connectVNC } = require('../http/api')
const fs = require('fs')
const { createFirstMenu } = require('../utils/menu')
const { respondTableOk, respondOk } = require('../utils/responseTemplate')
const Qs = require("qs");　　//qs是一个url参数转化（parse/stringify）的js库


router.prefix('/cloudtest/api')
let token = fs.readFileSync('./cache', 'utf-8') || null

/**
 * 登录接口
 */
router.post('/sys/login', async function (ctx, next) {
  const params = ctx.request.body
  console.log('params', params);
  const data = await login(params)
  token = data.access_token
  fs.writeFileSync('./cache', token)
  ctx.body = {
    code: 200,
    result: {
      token: data.access_token,
      userInfo: {
        username: data.user.username
      }
    }
  }
})


/**
 * 获取权限接口
 */
 router.get('/sys/permission/getUserPermissionByToken', function (ctx, next) {
  ctx.body = {
    "success": true,
    "message": "查询成功",
    "code": 200,
    "result": {
      "allAuth": [{
        "action": "online:goGenerateCode",
        "describe": "代码生成按钮",
        "type": "1",
        "status": "1"
      }, {
        "action": "user:add",
        "describe": "添加按钮",
        "type": "1",
        "status": "1"
      }, {
        "action": "user:edit",
        "describe": "用户编辑",
        "type": "1",
        "status": "1"
      }, {
        "action": "user:sex",
        "describe": "表单性别可见",
        "type": "1",
        "status": "1"
      }, {
        "action": "user:form:birthday",
        "describe": "禁用生日字段",
        "type": "2",
        "status": "1"
      }, {
        "action": "user:form:phone",
        "describe": "手机号禁用",
        "type": "2",
        "status": "1"
      }],
      "auth": [{
        "action": "online:goGenerateCode",
        "describe": "代码生成按钮",
        "type": "1"
      }, {
        "action": "user:add",
        "describe": "添加按钮",
        "type": "1"
      }],
      "menu": [
        // createFirstMenu('首页', "/dashboard/analysis", "dashboard/Analysis"),
        createFirstMenu('首页', "home/home", "home/Home"),
        // createFirstMenu('虚拟机管理', "/servers/serverlist", "servers/ServersList"),
        // createFirstMenu('镜像管理', "/imagelist/imagelist", "imagelist/ImageList"),
        // createFirstMenu('网络管理', "/network/networklist", "network/NetworkList"),
        // createFirstMenu('路由管理', "/router/routerlist", "router/RouterList"),
        // createFirstMenu('项目详情', "project-detail", "projectDetail/ProjectDetail"),
        createFirstMenu('项目列表', "project-list", "projectList/ProjectList"),
        // createFirstMenu('配置项目环境', "create-env", "createEnv/CreateEnv"),
        {
          "redirect": null,
          "path": "/projects",
          "component": "layouts/RouteView",
          "route": "1",
          "hidden": true,
          "children": [
            {
              "path": "project-detail",
              "component": "projectDetail/ProjectDetail",
              "route": "1",
              "meta": {
                "keepAlive": false,
                "internalOrExternal": false,
                "componentName": "Index",
                "title": "项目详情"
              },
              "name": "project-detail",
              "id": "d86f58e7ab516d3bc6bfb1fe10585f97"
            },
            {
              "path": "create-env",
              "component": "createEnv/CreateEnv",
              "route": "1",
              "meta": {
                "keepAlive": false,
                "internalOrExternal": false,
                "componentName": "Index",
                "title": "配置测试环境"
              },
              "name": "create-env",
              "id": "d86f58e7ab516d3bc6bfb1fe10585f95"
            },
          ],
          "meta": {
            "keepAlive": false,
            "internalOrExternal": false,
            "icon": "user",
            "componentName": "RouteView",
            "title": "个人页"
          },
          "name": "account",
          "id": "717f6bee46f44a3897eca9abd6e2ec44"
        }
      ]
    },
    "timestamp": 1624330126166
  }
})

/**
 * 首页接口
 */
router.get('/home', async function (ctx, next) {
  const projects = [
    {
      id:'2323352542523',
      name:"xiezh01",
      description:"xxxxxxx"
    },
    {
      id:'2323352542523',
      name:"xiezh02",
      description:"sssssxxxxxxx"
    },
    {
      id:'2323352542523',
      name:"xiezh03",
      description:"wwwwwxxxxxxx"
    }, 
  ]
  ctx.body = respondOk(projects)
})

/**
 * 项目列表接口
 */
 router.get('/projects', async function (ctx, next) {
  const projects = [
    {
      id:'2323352542523',
      name:"xiezh01",
      description:"xxxxxxx"
    },
    {
      id:'2323352542523',
      name:"xiezh02",
      description:"sssssxxxxxxx"
    },
    {
      id:'2323352542523',
      name:"xiezh03",
      description:"wwwwwxxxxxxx"
    }, 
  ]
  ctx.body = respondOk(projects)
})

/**
 * 项目详情接口
 */
router.get('/projects/detail', async function (ctx, next) {
  console.log('参数：',ctx.request.query);
  const detail = {
    id:"234234234",
    name:'xiezh',
    description:'xxxxxxxxxxxxx',
    envInfo:{
      cpu:2,
      ram:16,
      vms:[
        {
          id:'1231312312',
          vmName:'镜像名称1',
          status:'SHUTOFF',
          flavor:'1c1g23g',
          IPAddress:{
            public:'192.168.1.110',
            private:'110.168.1.110',
          },
        }
      ],
    },
  }
  ctx.body = respondOk(detail)
})

/**
 * 查询VNC界面的地址
 */
 router.get('/vm/connect', async function (ctx, next) {
  const id = ctx.request.query.id
  console.log('参数：',id);
  // const res = await connectVNC(id, token)
  // // console.log(data);
  // if (res.code === 0) {
  //   ctx.body = respondOk(res)
  // } else {
  //   ctx.body = respondOk(res)
  // }
    ctx.body = respondOk({url:'https://www.baidu.com'})

})

/**
 * 重启接口
 */
router.get('/vm/reboot', async function (ctx, next) {
  // console.log(data);
  const id = ctx.request.query
  console.log('参数：',id);
  ctx.body = respondOk()

})

/**
 * 关机接口
 */
 router.get('/vm/shutdown', async function (ctx, next) {
  // console.log(data);
  const id = ctx.request.query
  console.log('参数：',id);
  ctx.body = respondOk()

})

/**
 * 删除项目接口
 */
 router.delete('/projects', async function (ctx, next) {
  // console.log(data);
  const id = ctx.request.query
  console.log('参数：',id);
  ctx.body = respondOk()

})

/**
 * 批量删除项目接口
 */
 router.delete('/projects/deleteBatch', async function (ctx, next) {
  // console.log(data);
  const id = ctx.request.query
  console.log('参数：',id);
  ctx.body = respondOk()

})

/**
 * 修改项目状态接口
 */
router.put('/projects/status', async function (ctx, next) {
  const id = ctx.request.body
  console.log('参数：',id);
  ctx.body = respondOk()

})

/**
 * 新建项目接口
 */
router.post('/projects/create', async function (ctx, next) {
  const id = ctx.request.body
  console.log('参数：',id);
  ctx.body = respondOk()
})

/**
 * 获取推荐配置接口
 */
router.post('/vm/recommend', async function (ctx, next) {
  const id = ctx.request.body
  console.log('参数：',id);
  ctx.body = respondOk({
    recommend:[
      {
        imageId:"423423421",
        cpu:2,
        ram:7,
        os: "win10",
        storage: 512
      },
      {
        imageId:"423423423",
        cpu:2,
        ram:7,
        os: "win10",
        storage: 512
      },
      {
        imageId:"423423523",
        cpu:2,
        ram:7,
        os: "win10",
        storage: 512
      },
    ]
  })
})


/**
 * 创建虚拟机接口
 */
 router.post('/vm/create', async function (ctx, next) {
  const id = ctx.request.body
  console.log('参数：',id);
  ctx.body = respondOk()
})


module.exports = router
